import { NextRequest, NextResponse } from 'next/server';
import { UserModel } from '@/models/user';
import { hashPassword } from '@/utils/auth';

export async function GET(request: NextRequest) {
  try {
    // 生成随机用户名以避免冲突
    const randomSuffix = Math.floor(Math.random() * 10000);
    const testUser = {
      username: `测试用户${randomSuffix}`,
      password: await hashPassword('123456'),
      email: `test${randomSuffix}@example.com`,
      phone: '13800138000',
      location: '北京市',
      company: '测试公司'
    };
    
    console.log('尝试创建测试用户:', testUser.username);
    
    // 创建用户
    const userId = await UserModel.create(testUser);
    
    return NextResponse.json({
      status: 'success',
      message: '测试用户创建成功',
      userId,
      username: testUser.username,
      password: '123456', // 明文密码，仅用于测试
      email: testUser.email,
      timestamp: new Date().toISOString()
    });
  } catch (error) {
    console.error('测试用户创建失败:', error);
    
    return NextResponse.json({
      status: 'error',
      message: '测试用户创建失败',
      error: error instanceof Error ? error.message : '未知错误',
      timestamp: new Date().toISOString()
    }, { status: 500 });
  }
}
